#[cfg(test)]
mod tests {
    use crate::HttpTransport;
    use rustcloud_core::{Transport, Request, ServiceInstance, TransportConfig};
    use wiremock::{MockServer, Mock, ResponseTemplate};
    use wiremock::matchers::{method, path, header, body_string_contains};
    
    #[tokio::test]
    async fn test_http_transport_creation() {
        let transport = HttpTransport::new();
        assert_eq!(transport.name(), "http");
    }
    
    #[tokio::test]
    async fn test_http_transport_with_config() {
        let config = TransportConfig {
            timeout: 1000,
            retries: 2,
            retry_delay: 500,
            max_connections: 50,
            keep_alive: true,
        };
        let transport = HttpTransport::with_config(config);
        assert_eq!(transport.name(), "http");
    }
    
    #[tokio::test]
    async fn test_http_get_request() {
        let mock_server = MockServer::start().await;
        
        Mock::given(method("GET"))
            .and(path("/api/test"))
            .respond_with(ResponseTemplate::new(200).set_body_string("success"))
            .mount(&mock_server)
            .await;
        
        let transport = HttpTransport::new();
        let mut request = Request::new("test-service", "GET", "/api/test");
        request.add_header("Accept".to_string(), "application/json".to_string());
        
        let instance = ServiceInstance::new(
            "test-service",
            "127.0.0.1",
            mock_server.address().port(),
            "http"
        );
        
        let response = transport.send(request, &instance).await;
        assert!(response.is_ok());
        let resp = response.unwrap();
        assert_eq!(resp.status, 200);
        assert_eq!(String::from_utf8(resp.body.unwrap()).unwrap(), "success");
    }
    
    #[tokio::test]
    async fn test_http_post_request() {
        let mock_server = MockServer::start().await;
        
        Mock::given(method("POST"))
            .and(path("/api/create"))
            .and(header("content-type", "application/json"))
            .and(body_string_contains("test"))
            .respond_with(ResponseTemplate::new(201).set_body_string("created"))
            .mount(&mock_server)
            .await;
        
        let transport = HttpTransport::new();
        let mut request = Request::new("test-service", "POST", "/api/create");
        request.add_header("Content-Type".to_string(), "application/json".to_string());
        request.set_body(b"{\"name\":\"test\"}".to_vec());
        
        let instance = ServiceInstance::new(
            "test-service",
            "127.0.0.1",
            mock_server.address().port(),
            "http"
        );
        
        let response = transport.send(request, &instance).await;
        assert!(response.is_ok());
        let resp = response.unwrap();
        assert_eq!(resp.status, 201);
        assert_eq!(String::from_utf8(resp.body.unwrap()).unwrap(), "created");
    }
    
    #[tokio::test]
    async fn test_http_put_request() {
        let mock_server = MockServer::start().await;
        
        Mock::given(method("PUT"))
            .and(path("/api/update"))
            .respond_with(ResponseTemplate::new(200).set_body_string("updated"))
            .mount(&mock_server)
            .await;
        
        let transport = HttpTransport::new();
        let request = Request::new("test-service", "PUT", "/api/update");
        
        let instance = ServiceInstance::new(
            "test-service",
            "127.0.0.1",
            mock_server.address().port(),
            "http"
        );
        
        let response = transport.send(request, &instance).await;
        assert!(response.is_ok());
        let resp = response.unwrap();
        assert_eq!(resp.status, 200);
    }
    
    #[tokio::test]
    async fn test_http_delete_request() {
        let mock_server = MockServer::start().await;
        
        Mock::given(method("DELETE"))
            .and(path("/api/delete"))
            .respond_with(ResponseTemplate::new(204))
            .mount(&mock_server)
            .await;
        
        let transport = HttpTransport::new();
        let request = Request::new("test-service", "DELETE", "/api/delete");
        
        let instance = ServiceInstance::new(
            "test-service",
            "127.0.0.1",
            mock_server.address().port(),
            "http"
        );
        
        let response = transport.send(request, &instance).await;
        assert!(response.is_ok());
        let resp = response.unwrap();
        assert_eq!(resp.status, 204);
    }
    
    #[tokio::test]
    async fn test_http_request_with_query_params() {
        let mock_server = MockServer::start().await;
        
        Mock::given(method("GET"))
            .and(path("/api/search"))
            .respond_with(ResponseTemplate::new(200).set_body_string("results"))
            .mount(&mock_server)
            .await;
        
        let transport = HttpTransport::new();
        let mut request = Request::new("test-service", "GET", "/api/search");
        request.add_query_param("q".to_string(), "test".to_string());
        request.add_query_param("limit".to_string(), "10".to_string());
        
        let instance = ServiceInstance::new(
            "test-service",
            "127.0.0.1",
            mock_server.address().port(),
            "http"
        );
        
        let response = transport.send(request, &instance).await;
        assert!(response.is_ok());
        let resp = response.unwrap();
        assert_eq!(resp.status, 200);
    }
    
    #[tokio::test]
    async fn test_http_error_response() {
        let mock_server = MockServer::start().await;
        
        Mock::given(method("GET"))
            .and(path("/api/error"))
            .respond_with(ResponseTemplate::new(500).set_body_string("Internal Server Error"))
            .mount(&mock_server)
            .await;
        
        // Use a transport with no retries for this test
        let config = TransportConfig {
            retries: 0, // No retries for error responses
            timeout: 5000,
            retry_delay: 1000,
            max_connections: 100,
            keep_alive: true,
        };
        let transport = HttpTransport::with_config(config);
        let request = Request::new("test-service", "GET", "/api/error");
        
        let instance = ServiceInstance::new(
            "test-service",
            "127.0.0.1",
            mock_server.address().port(),
            "http"
        );
        
        let response = transport.send(request, &instance).await;
        assert!(response.is_ok());
        let resp = response.unwrap();
        assert_eq!(resp.status, 500);
        assert!(!resp.is_success());
    }
    
    #[tokio::test]
    async fn test_http_unsupported_method() {
        let transport = HttpTransport::new();
        let request = Request::new("test-service", "PATCH", "/api/test");
        
        let instance = ServiceInstance::new("test-service", "127.0.0.1", 8080, "http");
        
        // PATCH is supported, let's test an actually unsupported method
        let mut unsupported_request = request.clone();
        unsupported_request.method = "CUSTOM".to_string();
        
        let response = transport.send(unsupported_request, &instance).await;
        assert!(response.is_err());
    }
    
    #[tokio::test]
    async fn test_http_batch_requests() {
        let mock_server = MockServer::start().await;
        
        Mock::given(method("GET"))
            .and(path("/api/test1"))
            .respond_with(ResponseTemplate::new(200).set_body_string("response1"))
            .mount(&mock_server)
            .await;
            
        Mock::given(method("GET"))
            .and(path("/api/test2"))
            .respond_with(ResponseTemplate::new(200).set_body_string("response2"))
            .mount(&mock_server)
            .await;
        
        let transport = HttpTransport::new();
        let request1 = Request::new("test-service", "GET", "/api/test1");
        let request2 = Request::new("test-service", "GET", "/api/test2");
        
        let instance = ServiceInstance::new(
            "test-service",
            "127.0.0.1",
            mock_server.address().port(),
            "http"
        );
        
        let requests = vec![(request1, &instance), (request2, &instance)];
        let responses = transport.send_batch(requests).await;
        
        assert_eq!(responses.len(), 2);
        assert!(responses[0].is_ok());
        assert!(responses[1].is_ok());
        
        let resp1 = responses[0].as_ref().unwrap();
        let resp2 = responses[1].as_ref().unwrap();
        assert_eq!(resp1.status, 200);
        assert_eq!(resp2.status, 200);
    }
    
    #[tokio::test]
    async fn test_invalid_url() {
        let transport = HttpTransport::new();
        let request = Request::new("test-service", "GET", "/api/test");
        
        // Invalid host format
        let instance = ServiceInstance::new("test-service", "invalid host format", 8080, "http");
        
        let response = transport.send(request, &instance).await;
        assert!(response.is_err());
    }
}